<!DOCTYPE html>
<html lang="zh" style="background-color: #fff;" >
<head>
<meta charset="utf-8" />
<title>数据门户指标定义</title> 
<link href="${mvcPath}/dacp-lib/bootstrap/css/bootstrap.min.css" type="text/css" rel="stylesheet" media="screen"/>	
<link href="${mvcPath}/dacp-view/aijs/css/ai.css" type="text/css" rel="stylesheet" />
<link href="${mvcPath}/dacp-res/catalog/common/css/ai.meta.css" type="text/css" rel="stylesheet"/>
<link href="${mvcPath}/dacp-lib/fuelux/fuelux.min.css" type="text/css" rel="stylesheet" media="screen"/>
<script type="text/javascript" src="${mvcPath}/dacp-lib/jquery/jquery-1.10.2.min.js"></script>
<script type="text/javascript" src="${mvcPath}/dacp-lib/bootstrap/js/bootstrap.min.js"></script>
<script type="text/javascript" src="${mvcPath}/dacp-lib/underscore/underscore-min.js"></script>
<script src="${mvcPath}/dacp-lib/fuelux/fuelux.min.js"></script>
<script src="${mvcPath}/dacp-lib/cryptojs/aes.js" type="text/javascript"></script>
<script src="${mvcPath}/crypto/crypto-context.js" type="text/javascript"></script>
<script src="${mvcPath}/dacp-view/aijs/js/ai.core.js"></script>
<script src="${mvcPath}/dacp-view/aijs/js/ai.field.js"></script>
<script src="${mvcPath}/dacp-view/aijs/js/ai.jsonstore.js"></script>
<script src="${mvcPath}/dacp-view/aijs/js/ai.grid.ux.js"></script>
<style>
</style>
<script>
$(document).ready(function() {
	var sql = "select KPI_ID,KPI_CODE,KPI_NAME,KPI_TYPE,KPI_SCOPE_CODE,KPI_SQL,IS_VALID from dacp_kpi_def";
	var portalKpiStore = new AI.JsonStore({
		sql:sql,
		pageSize:20,
		table:"dacp_kpi_def",
		key:"KPI_ID"
	});
	var items;
	function buildItems(x){
		items = [{
        type: 'text',
        label: '指标编码',
        isReadOnly:x,
        notNull: 'N',
        fieldName: 'KPI_CODE'
	    },{
	        type: 'text',
	        label: '指标名称',
	        isReadOnly:x,
	        notNull: 'N',
	        fieldName: 'KPI_NAME'
	    },{
	        type: 'combox',
	        label: '指标类型',
	        isReadOnly:x,
	        notNull: 'N',
	        fieldName: 'KPI_TYPE',
	        storesql:'online,线上|offline,线下'
	    },{
	        type: 'text',
	        label: '指标组编码',
	        isReadOnly:x,
	        notNull: 'Y',
	        fieldName: 'KPI_SCOPE_CODE'
	    },{
	        type: 'combox',
	        label: '是否生效',
	        isReadOnly:x,
	        notNull: 'N',
	        fieldName: 'IS_VALID',
	        storesql:'0,生效|1,失效'
	    },{
	        type: 'textarea',
	        label: '口径',
	        isReadOnly:x,
	        notNull: 'N',
	        fieldName: 'KPI_SQL'
	    }];
	}
	
	var iscreat;
	var queryNotice = function(){
		var qtitle = $("#q_notice_title").val();
		var newsql = "select KPI_ID,KPI_CODE,KPI_NAME,KPI_TYPE,KPI_SCOPE_CODE,KPI_SQL,IS_VALID from dacp_kpi_def where KPI_CODE like '%"+qtitle+"%' or KPI_NAME like '%"+qtitle+"%'";
		portalKpiStore.select(newsql);
		portalKpiList.reload();
		eachtr();
	};
	var addNotice = function(){
		iscreat = true;
		portalKpiStore.select();
		var rec = portalKpiStore.getNewRecord();
		portalKpiStore.add(rec);
		rec.set("KPI_ID",ai.guid());
		portalKpiStore.curRecord = rec;
		buildItems('n');
		ai.openFormDialog("新增指标",items,callBack,portalKpiStore);
	};
	var curkpicode;
	var updateNotice = function(){
		var obj=$(".repeater-list tr.datarow.active")[0];
		if(!obj){
			alert('请选择一行记录!');
			return;
		}
		iscreat = false;
		//var rec = portalKpiStore.curRecord;
		//var key = rec.get("KPI_ID");
		//portalKpiStore.select();
		//portalKpiStore.curRecord = portalKpiStore.getRecordByKey(key);
		curkpicode = portalKpiStore.curRecord.get("KPI_CODE");
		buildItems('n');
		ai.openFormDialog("修改指标",items,callBack,portalKpiStore);
	};
	var deleteNotice = function(){
		var obj=$(".repeater-list tr.datarow.active")[0];
		if(!obj){
			alert('请选择一行记录!');
			return;
		}
		var rec = portalKpiStore.curRecord;
		var result = confirm("是否删除选择项?");
		if(result){
			portalKpiStore.remove(rec);
			portalKpiStore.commit();
			portalKpiList.reload();
			eachtr();
		}
	};
	var callBack = function(){
		var boo = true;
		var kpicode = $("#KPI_CODE").val();
		var kpiname = $("#KPI_NAME").val();
		var kpitype = $("#KPI_TYPE").val();
		var kpisql = $("#KPI_SQL").val();
		var isvalid = $("#IS_VALID").val();
		if(kpicode&&kpiname&&kpitype&&kpisql&&isvalid){
			if(iscreat){
				var isexist = ai.getStoreData("select KPI_CODE from dacp_kpi_def where KPI_CODE='"+kpicode+"'");
				if(isexist.length>0){
					boo=false;
					alert("此编码已被占用!");
				}
			}
			if(!iscreat){
				if(kpicode!=curkpicode){
					var isexist = ai.getStoreData("select KPI_CODE from dacp_kpi_def where KPI_CODE='"+kpicode+"'");
					if(isexist.length>0){
						boo=false;
						alert("此编码已被占用11!");
					}
				}
			}
		}else{
			alert("请填写带*的项!");
			boo=false;
		}
		if(boo){
			portalKpiStore.commit();
			portalKpiList.reload();
			eachtr();
		}else{
			return boo;
		}
	};
	var config = {
		containerId:"portalkpiinfo",
		title:"数据门户指标",
		store:portalKpiStore,
		show:true,
		showcheck:false, 
		viewtype:"list",
		card:{},
		columns:[
			{header: "指标编码", dataIndex: "KPI_CODE", headerAlign: "center",width: "15%",sortable:true},
			{header: "指标名称", dataIndex: "KPI_NAME", headerAlign: "center",width: "15%",sortable:true},
			{header: "指标类型", dataIndex: "KPI_TYPE", headerAlign: "center", dataAlign: "center", width: "10%",sortable:true },
			{header: "指标组编码", dataIndex: "KPI_SCOPE_CODE", headerAlign: "center",width: "15%",sortable:true},
			{header: "是否生效", dataIndex: "IS_VALID", headerAlign: "center",width: "10%",sortable:true},
			{header: "口径", dataIndex: "KPI_SQL", headerAlign: "center",width: "35%",sortable:true}
		],
		toolbarconfig:[
   			{type:'text',fieldLabel:'输入指标编码或名称',width:120,name:'q_notice_title'}
   			,{type:'button',fieldLabel:'查询',id:'queryNotice',width:120,fn:queryNotice}
   			,{type:'button',fieldLabel:'新建',id:'addNotice',width:120,fn:addNotice}
   			,{type:'button',fieldLabel:'修改',id:'updateNotice',width:120,fn:updateNotice}
   			,{type:'button',fieldLabel:'删除',id:'updateNotice',width:120,fn:deleteNotice}
     	]
	};
	var portalKpiList = new AI.GridUx(config);
	var callBack1 = function(){};
	portalKpiList.on("dblclick",function(){
		buildItems('y');
		ai.openFormDialog("指标详细信息",items,callBack1,portalKpiStore);
	});
	function eachtr(){
		$("table tbody tr").each(function(){
			var td = $(this).find("td:eq(5)");
			var tdtext = td.text();
			if(tdtext.length>60){
				var newtext = tdtext.substring(0,60);
				td.text(newtext+"...");
			}
		});
	}
	eachtr();
	
});
</script>
</head>
<body  style="height:100%">
	<div class="frame-main" style="top:0px;position: absolute;left: 0px;right: 0px;bottom:0px;overflow:hidden">
       <div id="portalkpiinfo"  style="height:100%;"></div>
	</div>
</body>
</html>